package com.cph.project.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.cph.common.util.DateUtils;
import com.cph.tool.logUpdate.Base;
import com.cph.tool.logUpdate.UnityLog;
import com.cph.tool.logUpdate.mapper.UnityLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cph.project.mapper.SysUserMapper;
import com.cph.project.pojo.SysUser;
import com.cph.project.service.ISysUserService;

/**
 * 用户信息Service业务层处理
 *
 * @author cph
 * @date 2020-02-26
 */
@Service
@Slf4j
public class SysUserServiceImpl implements ISysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private UnityLogMapper unityLogMapper;
    /**
     * 查询用户信息
     *
     * @param userId 用户信息ID
     * @return 用户信息
     */
    @Override
    public SysUser selectSysUserById(Long userId) {
        return sysUserMapper.selectSysUserById(userId);
    }

    /**
     * 查询用户信息列表
     *
     * @param sysUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<SysUser> selectSysUserList(SysUser sysUser) {
        return sysUserMapper.selectSysUserList(sysUser);
    }

    /**
     * 新增用户信息
     *
     * @param sysUser 用户信息
     * @return 结果
     */
    @Override
    public int insertSysUser(SysUser sysUser) {
        sysUser.setCreateTime(DateUtils.getNowDate());
        return sysUserMapper.insertSysUser(sysUser);
    }

    /**
     * 修改用户信息
     *
     * @param sysUser 用户信息
     * @return 结果
     */
    @Override
    public int updateSysUser(SysUser sysUser) {
        try {
            SysUser sysUserOld = sysUserMapper.selectSysUserById(sysUser.getUserId());
            UnityLog logOpe = sysUser.createLog(Base.UPDATE, sysUserOld);
            logOpe.setUnityOperateTime(DateUtils.getNowDate());
            logOpe.setUnityOperator("cph");
            logOpe.setUnityTagId(sysUserOld.getUserId().toString());
            log.info("updateSysUser unityLog value is ={}", JSON.toJSONString(logOpe));
            unityLogMapper.insertOaUnityLog(logOpe);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        sysUser.setUpdateTime(DateUtils.getNowDate());
        return sysUserMapper.updateSysUser(sysUser);
    }

    /**
     * 批量删除用户信息
     *
     * @param userIds 需要删除的用户信息ID
     * @return 结果
     */
    @Override
    public int deleteSysUserByIds(Long[] userIds) {
        return sysUserMapper.deleteSysUserByIds(userIds);
    }

    /**
     * 删除用户信息信息
     *
     * @param userId 用户信息ID
     * @return 结果
     */
    @Override
    public int deleteSysUserById(Long userId) {
        return sysUserMapper.deleteSysUserById(userId);
    }
}
